# -*- coding:utf-8 -*-
#Python
#Generator: PyCharm
#Author: Zhong


#import traceback
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from src import filePath
from datetime import datetime

class Base(webapp.RequestHandler):
	def __init__(self):
		self.errorMsg = ''
		self.emptyFolder = '_$folder$'
		self.views = filePath('../views/')
		super(Base, self).__init__()

	def write(self, str):
		self.response.out.write(str)

	def render(self, template_file, template_vals):
		f = self.views + template_file
		self.response.out.write(template.render(f, template_vals))

	def param(self, name, **kw):
		return self.request.get(name, **kw)


	@classmethod
	def doPost(cls, method):
		def _doPost(self,*args, **kwargs):
			if self.request.method != 'POST':
				return self.error(403)
			return method(self, *args, **kwargs)
		return _doPost

	def error(self, errorcode):
		'''
		if errorcode == 404:
			message = 'Sorry, we were not able to find the requested page.  We have logged this error and will look into it.'
		elif errorcode == 403:
			message = 'Sorry, that page is reserved for administrators.  '
		elif errorcode == 500:
			message = "Sorry, the server encountered an error.  We have logged this error and will look into it."

		message += "\n <p> %s </p>" % self.errorMsg
		'''
		if errorcode > 0:
			self.response.set_status(errorcode)
		#super(Base, self).error(errorcode)

		errorMsg = self.errorMsg if self.errorMsg else 'error'
		self.response.out.write(errorMsg)



class History(db.Model):
	id_ = db.StringProperty('id')
	key_ = db.StringProperty('key')
	ip = db.StringProperty()
	visitTime = db.StringProperty(default = str(datetime.now()))

	def __repr__(self):
		return self.id_

	def toString(self):
		return "id: %s, key: %s, ip: %s, visitTime: %s" % (self.id_, self.key_, self.ip, self.visitTime)